/**
 * @param {string} s
 * @return {boolean}
 */

var isNumber = function(s) {

  // 状态枚举
  const   STATE_START_INTEGER = 0     //0. 起始数字
  const   STATE_INTEGER = 1           //1. 整数部分
  const   STATE_POINT = 2             //2. 小数点
  const   STATE_FRACTION = 3          //3. 小数部分
  const   STATE_EXP = 4               //4. 字符 E e
  const   STATE_EXP_SIGN = 5          //5. 指数部分的符号位
  const   STATE_EXP_NUMBER = 6        //6. 指数部分的整数部分

  // 字符枚举
  const   CHAR_NUMBER = 1         // 0-9
  const   CHAR_EXP = 2            // E e
  const   CHAR_POINT = 3          // .
  const   CHAR_SIGN = 4           // + -
  const   CHAR_ILLEGAL = 5        // 除上面字符外都是非法字符


  const toCharType = char => {
    switch(char) {
      case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8':case '9':  return CHAR_NUMBER
      case 'E': case 'e': return CHAR_EXP
      case '.': return CHAR_POINT
      case '+': case '-': return CHAR_SIGN
      default: return CHAR_ILLEGAL
    }
  }

  // 状态转移表
  const tranfer = {
    // 起始数字
    [STATE_START_INTEGER]: {
      [CHAR_NUMBER]: STATE_INTEGER,
    },
    // 数字位
    [STATE_INTEGER]: {
      [CHAR_NUMBER]: STATE_INTEGER,
      [CHAR_EXP]: STATE_EXP,
      [CHAR_POINT]: STATE_POINT,
    },
    // 点
    [STATE_POINT]: {
      [CHAR_NUMBER]: STATE_FRACTION,
      [CHAR_EXP]: STATE_EXP,
    },
    // 小数数字
    [STATE_FRACTION]: {
      [CHAR_NUMBER]: STATE_FRACTION,
      [CHAR_EXP]: STATE_EXP,
    },
    // 指数
    [STATE_EXP]: {
      [CHAR_NUMBER]: STATE_EXP_NUMBER,
      [CHAR_SIGN]: STATE_EXP_SIGN,
    },
    // 指数符号
    [STATE_EXP_SIGN]: {
      [CHAR_NUMBER]: STATE_EXP_NUMBER,
    },
    // 指数数字
    [STATE_EXP_NUMBER]: {
      [CHAR_NUMBER]: STATE_EXP_NUMBER,
    },
  }

  const len = s.length
  let state = STATE_START_INTEGER // 初始状态

  // 状态转移路径
  for (let i = 0; i < len; i++) {
    let ch = s[i]
    let chType = toCharType(ch)
    let nextState = tranfer[state][chType]
    if (nextState) {
      state = nextState
    } else {
      return false
    }
  }

  // 注意遍历完字符串后最后可能的状态
  return state === STATE_INTEGER
    || state === STATE_FRACTION
    || state === STATE_EXP_NUMBER
};


/**
 * @param {string} s
 * @return {Array}
*/
var findNumber = function(s) {
  const ans = new Array(s.length).fill('-');
  // 两层遍历判断是否是数字，并将数字字符串存储在 ans 中。
  for(let i = 0; i < s.length; i++) {
    for (let j = i; j < s.length; j++) {
      if (isNumber(s.substring(i, j + 1))) {
        ans[i] = s.substring(i, j + 1);
      }
    }
    i += (ans[i].length - 1);
  }
  return ans.filter(item => item !== '-');
}

const str = 'a1.23E+a4.5E6b78.c';
console.log(findNumber(str));